#include "UIButton.h"

UIButton::UIButton():
m_pNormalSprite(0),
m_pSelectedSprite(0),
m_pDisabledSprite(0),
m_bSelected(false),
m_nActiveHandler(0),
m_pListener(0),
m_pfnSelector(0),
m_bCheckable(false),
m_bChecked(false)
{

}

UIButton::~UIButton()
{

}

UIButton* UIButton::create(const char* normalFile, const char* selectedFile/* =NULL */, const char* disabledFile/* =NULL */, CCObject* target/* =NULL */, SEL_MenuHandler selector/* =NULL */)
{
	UIButton *pRet = new UIButton();
	if (pRet && pRet->initWithNormalImage(normalFile, selectedFile, disabledFile))
	{
		pRet->autorelease();
		return pRet;
	}
	CC_SAFE_DELETE(pRet);
	return NULL;
}


void UIButton::setTarget(CCObject *rec, SEL_MenuHandler selector)
{
	m_pListener = rec;
	m_pfnSelector = selector;
}

bool UIButton::initWithNormalImage(const char* normalFile, const char* selectedFile/* =NULL */, const char* disabledFile/* =NULL */, CCObject* target/* =NULL */, SEL_MenuHandler selector/* =NULL */)
{
	if (normalFile)
	{
		setNormalSprite(CCSprite::create(normalFile));
	}
	if (selectedFile)
	{
		setSelectedSprite(CCSprite::create(selectedFile));
	}
	if (disabledFile)
	{
		setSelectedSprite(CCSprite::create(disabledFile));
	}

	setTarget(target, selector);

	init();

	if (m_pNormalSprite)
	{
		setContentSize(m_pNormalSprite->getContentSize());
	}
	else 
	{
		return false;
	}

	return true;
}

void UIButton::draw()
{
	if (!m_bCheckable)
	{
		if (!m_bEnable && m_pDisabledSprite)
		{
			m_pDisabledSprite->draw();
		}
		else if (m_bSelected && m_pSelectedSprite)
		{
			m_pSelectedSprite->draw();
		}
		else
		{
			m_pNormalSprite->draw();
		}
	}
	else
	{
		if (!m_bEnable && m_pDisabledSprite)
		{
			m_pDisabledSprite->draw();
		}
		else if (m_bChecked && m_pSelectedSprite)
		{
			m_pSelectedSprite->draw();
		}
		else
		{
			m_pNormalSprite->draw();
		}
	}
}

void UIButton::activate()
{
	if (m_bEnable)
	{
		if (m_pListener && m_pfnSelector)
		{
			(m_pListener->*m_pfnSelector)(this);
		}

		if (m_nActiveHandler)
		{
		}
	}
}

void UIButton::registerScriptActiveHandler(int nHandler)
{
	unregisterScriptActiveHandler();
	m_nActiveHandler = nHandler;
	LUALOG("[LUA] Add UIButton script handler: %d", m_nScriptHandler);
}

void UIButton::unregisterScriptActiveHandler(void)
{
	if (m_nActiveHandler)
	{
		m_nActiveHandler = 0;
	}
}


bool UIButton::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	CCPoint touchLocation = pTouch->locationInView();
	touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);

	CCPoint local = convertToNodeSpace(touchLocation);
	CCRect r = rect();
	r.origin = CCPointZero;

	if (CCRect::CCRectContainsPoint(r, local))
	{
		if (!m_bChecked)
		{
			setSelected(m_bEnable);
		}
		return true;
	}

	return false;
}

void UIButton::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
	UIWidget::ccTouchMoved(pTouch, pEvent);
}

void UIButton::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
	UIWidget::ccTouchEnded(pTouch, pEvent);

	if (m_bEnable)
	{
		activate();
	}

	if (!m_bCheckable)
	{
		setSelected(false);
	}
	else
	{
		CCPoint touchLocation = pTouch->locationInView();
		touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);

		CCPoint local = convertToNodeSpace(touchLocation);
		CCRect r = rect();
		r.origin = CCPointZero;

		if (CCRect::CCRectContainsPoint(r, local))
		{
			if (m_bCheckable)
			{
				setChecked(!getChecked());
			}
		}
	}
}